#include "udp_sender.hpp"
#include <chrono>
#include <cstring>
#include <iostream>
#include <random>
#include <thread>
#include <vector>

int main() {
    UdpSender sender;
    const std::string serverIp = "172.16.31.38";
    const int serverPort = 5678;

    if (!sender.initialize(serverIp, serverPort)) {
        std::cerr << "Failed to initialize UDP sender" << std::endl;
        return 1;
    }

    const char *message = "Hello, UDP!";
    for (int i = 0; i < 100; i++) {
        if (!sender.sendData(message, strlen(message))) {
            std::cerr << "Failed to send message" << std::endl;
            return 1;
        }
        std::this_thread::sleep_for(std::chrono::milliseconds(100)); // 约 30 fps
    }

    std::cout << "Sent message: " << message << std::endl;


    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<> dis(0, 255);

    for (int i = 0; i < 100; ++i) {
        std::vector<uint8_t> frameData(100 * 1024);
        for (auto &byte : frameData) {
            byte = dis(gen);
        }

        if (!sender.sendData(frameData.data(), frameData.size())) {
            std::cerr << "Failed to send frame " << i << std::endl;
            return 1;
        }
        std::cout << "Sent frame " << i << " (" << frameData.size() << " bytes)" << std::endl;

        std::this_thread::sleep_for(std::chrono::milliseconds(33));
    }

    std::cout << "All test data sent successfully" << std::endl;

    return 0;
}